package com.atguigu.educenter.service.impl;

import com.atguigu.com.JwtUtils;
import com.atguigu.com.MD5;
import com.atguigu.com.exceptionhandler.GuliException;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.mapper.UcenterMemberMapper;
import com.atguigu.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2023-01-09
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Override
    public String login(UcenterMember ucenterMember) {
        //获取前端传入的手机号就和密码
        String mobile = ucenterMember.getMobile();
        String password = ucenterMember.getPassword();
        if (StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
            throw new GuliException(20001,"登陆失败1");
        }
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember member = baseMapper.selectOne(wrapper);
        if (member == null){
            throw new GuliException(20001,"登陆失败2");
        }
        System.out.println(MD5.encrypt(password));
        //加密密码进行比较
        if (!MD5.encrypt(password).equals(member.getPassword())){
            throw new GuliException(20001,"登陆失败3");
        }
        if (member.getIsDisabled()){
            throw new GuliException(20001,"登陆失败4");
        }
        //登录成功
        //生成token字符串，使用jwt工具类
        JwtUtils.getJwtToken(member.getId(), member.getNickname());
        return JwtUtils.getJwtToken(member.getId(), member.getNickname());
    }
}
